<template>
  <el-dialog
    :visible="isShow"
    :title="mode === 'add' ? '新增角色' : '编辑角色'"
    width="500px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model.trim="form.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model.trim="form.description" />
      </el-form-item>
    </el-form>
    <!-- dialog 底部插槽 -->
    <template #footer>
      <div>
        <el-button @click="closeEvent"> 取消 </el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRolePost, sysRolePut } from '@/api/setting'
export default {
  name: 'DialogItem',
  model: {
    prop: 'isShow'
  },
  props: {
    list: {
      type: Array,
      default: () => []
    },
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        name: '',
        description: ''
      },
      mode: 'add',
      initFrom: '',
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              // 新增验证
              let result = ''
              if (this.mode === 'add') {
                // 新增
                result = this.list.some((item) => {
                  return item.name === value
                })
              } else {
                // 修改
                result = this.list.some((item) => {
                  // 自定义验证排除当前项
                  return item.name === value && item.id !== this.form.id
                })
              }
              result ? callback(new Error('请不要重名')) : callback()
            },
            trigger: 'change'
          }
        ],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  created() {
    // 初始化数据 深拷贝一份原始数据
    this.initFrom = { ...this.form }
  },
  methods: {
    // 关闭弹框
    closeEvent() {
      // 表单还原
      this.$refs.form.resetFields()
      // 数据还原
      //   this.form = this.initFrom
      this.form = { ...this.initForm }
      //   console.log(this.form)
      //   console.log(this.initFrom)
      // 关闭弹窗
      // 。sync版
      //   this.$emit('update:isShow', false)
      // v-model 版
      this.$emit('input')
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 新增
          if (this.mode === 'add') {
            await sysRolePost(this.form)
            // 刷新父级列表数据
            this.$emit('currentChange', 1)
          } else {
            // 修改
            await sysRolePut(this.form)
          }
          // 消息提示
          this.$message.success(this.mode === 'add' ? '新增成功' : '修改成功')
          // 关闭弹窗
          this.closeEvent()
          // 刷新数据
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
